package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult<Brand> queryBrandsByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 分页条件
        if(rows.intValue() > 0){
            PageHelper.startPage(page, rows);
        }
        // 过滤
        /*
            WHERE name like %key% or letter == 'key'
            ORDER BY sortBy DESC
         */
        Example example = new Example(Brand.class);
        if(StringUtils.isNotBlank(key)){
            // 过滤条件
            example.createCriteria().orLike("name", "%"+key+"%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        // 排序
        if(StringUtils.isNotBlank(sortBy)){
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUNT);
        }
        // 解析分页结果
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
        return new PageResult<>(brandPageInfo.getTotal(), brandPageInfo.getPages(), brandList);
    }

    @Override
    @Transactional
    public void saveBrand(Brand brand, List<Long> ids) {
        brand.setId(null);
        int count = brandMapper.insertSelective(brand);
        if(count != 1 || CollectionUtils.isEmpty(ids)){
            throw new LyException(ExceptionEnum.ADD_BRAND_FAIL);
        }
        // 新增中间表
        ids.forEach(cid -> {
            int i = brandMapper.insertBrandAndCategory(cid, brand.getId());
            if(count != 1){
                throw new LyException(ExceptionEnum.ADD_BRAND_FAIL);
            }
        });
    }

    @Override
    @Transactional
    public void deleteBrand(Long bid) {
        // 删除关联表信息
        int count = brandMapper.deleteCategoryAndBrand(bid);
        if(count <= 0){
            throw new LyException(ExceptionEnum.BRAND_DELETE_FAIL);
        }
        // 删除品牌表信息
        count = brandMapper.deleteByPrimaryKey(bid);
        if(count <= 0){
            throw new LyException(ExceptionEnum.BRAND_DELETE_FAIL);
        }
    }

    @Override
    public List<Brand> queryBrandByCid(Long cid) {
        if(cid.intValue() <= 0 || cid == null){
            throw new LyException(ExceptionEnum.PARAMETERS_ILLEGAL_OR_NULL);
        }
        List<Brand> brands = this.brandMapper.selectBrandByCid(cid);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUNT);
        }
        return brands;
    }

    @Override
    public Brand queryBrandById(Long id) {
        if(id.intValue() <= 0 || id == null){
            throw new LyException(ExceptionEnum.PARAMETERS_ILLEGAL_OR_NULL);
        }
        Brand brand = this.brandMapper.selectByPrimaryKey(id);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUNT);
        }
        return brand;
    }
}
